<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends CommonController 
{
    //订单试课表
    public function index_sk()
    {
        //所有用户显示出来
        $jz_to_ls_yuyue = M('jz_to_ls_yuyue');        
        // 查询满足要求的总记录数
        $count =  $jz_to_ls_yuyue->count();
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        $sql = "select t1.id,t1.date,t1.time,t1.city_quyu,t1.grade,t1.subject,t1.is_laoshi_on,t1.is_shenhe,
                t2.sex as ls_sex,t2.sfz_name as ls_name,t2.phone as ls_phone,
                t3.sex as jz_sex,t3.name as jz_name,t3.phone as jz_phone 
                from jz_to_ls_yuyue t1 
                inner join laoshi_user t2 on t1.ls_uid=t2.id 
                inner join jiazhang_user t3 on t1.jz_uid=t3.id where t1.is_shenhe=0 limit ".$Page->firstRow.','.$Page->listRows;

        $order_shenhes = $jz_to_ls_yuyue->query($sql);
        $this->assign('order_shenhes',$order_shenhes);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
    //更换老师
    public function index_sk_hls()
    {
        if (IS_POST) {
            $id = I('post.id');
            $ls_uid = I('post.ls_uid');

            if (!is_numeric($ls_uid)) {
                 $arr = array(
                        'error' =>0,
                        'info'=>"请输入数字"
                        );
                echo json_encode($arr);
                exit;
            }

            $res = M('jz_to_ls_yuyue')->where(['id'=>$id])->setField('ls_uid',$ls_uid);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"更换成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"更换失败"
                        );
                echo json_encode($arr);
            }

        }else {
            $id = I('get.id');
            $laos_info = M('jz_to_ls_yuyue')->where(['id'=>$id])->find();
            $this->assign('laos_info',$laos_info);

            $this->display();
        }
    }

    //订单审核表
    public function index()
    {   
        //所有用户显示出来
        $jz_to_ls_yuyue = M('jz_to_ls_yuyue');        
        // 查询满足要求的总记录数
        $count =  $jz_to_ls_yuyue->count();
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        $sql = "select t1.id,t1.date,t1.time,t1.city_quyu,t1.grade,t1.subject,t1.zong_ks,t1.shengyu_ks,t1.is_shenhe,
                t2.sex as ls_sex,t2.sfz_name as ls_name,t2.phone as ls_phone,
                t3.sex as jz_sex,t3.name as jz_name,t3.phone as jz_phone 
                from jz_to_ls_yuyue t1 
                inner join laoshi_user t2 on t1.ls_uid=t2.id 
                inner join jiazhang_user t3 on t1.jz_uid=t3.id where t1.is_shenhe=1 limit ".$Page->firstRow.','.$Page->listRows;

        $order_shenhes = $jz_to_ls_yuyue->query($sql);
        $this->assign('order_shenhes',$order_shenhes);
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    /**
     * 导出 订单审核表所有数据
     */
    public function order_export(){
        $sql = "select t1.id,t1.date,t1.time,t1.city_quyu,t1.grade,t1.subject,t1.zong_ks,t1.shengyu_ks,t1.is_shenhe,
                t2.sex as ls_sex,t2.sfz_name as ls_name,t2.phone as ls_phone,
                t3.sex as jz_sex,t3.name as jz_name,t3.phone as jz_phone 
                from jz_to_ls_yuyue t1 
                inner join laoshi_user t2 on t1.ls_uid=t2.id 
                inner join jiazhang_user t3 on t1.jz_uid=t3.id";
        $data = M()->query($sql);

        $subject="Excel导出-订单审核表";
        $title=array("预约id","试课日期","试课时间","上课地址","年级","科目","总课时","剩余课时","审核状态","老师性别","老师","老师手机","家长性别","家长","家长手机");
        exportExcel($data,$title,$subject); 
    }

    //订单审核
    public function order_shenhe()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('jz_to_ls_yuyue')->where(array('id'=>$id))->setField('is_shenhe',1);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"审核成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"审核失败,重新在审核"
                        );
                echo json_encode($arr);
            }
        }
    }

    //编辑课时显示
    public function order_edit()
    {   
        $id = I('get.id');
        $sql = "select t1.id,t1.zong_ks,t1.shengyu_ks,t1.yishang_ks,
                t2.sfz_name as ls_name,t2.phone as ls_phone,
                t3.name as jz_name,t3.phone as jz_phone 
                from jz_to_ls_yuyue t1 
                inner join laoshi_user t2 on t1.ls_uid=t2.id 
                inner join jiazhang_user t3 on t1.jz_uid=t3.id where t1.id='{$id}'";
        $order_info = M()->query($sql);
        $this->assign('order_info',current($order_info));

        $this->display();
    }
    //更新课时
    public function edit()
    {   //Array ( [id] => 1 [zong_ks] => 50 [yishang_ks] => 2 )
        if (IS_POST) {
            $id = I('post.id');
            $zong_ks = I('post.zong_ks');
            $yishang_ks = I('post.yishang_ks');

            $shengyu_ks = $zong_ks - $yishang_ks;
            $data = array('zong_ks'=>$zong_ks,'shengyu_ks'=>$shengyu_ks,'yishang_ks'=>$yishang_ks);

            $res = M('jz_to_ls_yuyue')->where(array('id'=>$id))->setField($data);
            if ($res) {

                //剩余课时小于等于3 自动发信息到会员手机
                //家长ID
                $jz_uid = M('jz_to_ls_yuyue')->where(array('id'=>$id))->getField('jz_uid');
                //家长手机
                $js_phone = M('jiazhang_user')->where(array('id'=>$jz_uid))->getField('phone');
                if ($shengyu_ks < 4) {
                    kes_miaodi($js_phone);
                }

                 $arr = array(
                        'error' =>1,
                        'info'=>"更新课时成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"更新课时失败"
                        );
                echo json_encode($arr);
            }
        }
    }

    //签到表
    public function show()
    {
        $id = I('get.id');
        $signs = M('jz_sign')->where(['kec_id'=>$id])->select();
        $this->assign('signs',$signs);
        
        //每月老师家长正常签到数量统计
        $sql = "select addtime,FROM_UNIXTIME( `addtime`,'%Y-%m') as time ,count(*) as num ,sum(keshi) as keshi FROM jz_sign where jz_qd=1 group by time";
        $shows = M()->query($sql);
        $this->assign('shows',$shows);

        $this->display();
    }

    //通知家长签到
    public function tongzjz()
    {
        if (IS_POST) {
            $id = I('post.id');
            //查询授课签到信息
            $signs = M('jz_sign')->where(['id'=>$id])->find();
            //查询家长手机
            $jz_uid = M('jz_to_ls_yuyue')->where(['id'=>$signs['kec_id']])->getField('jz_uid');
            if ($jz_uid) {
                $js_phone = M('jiazhang_user')->where(array('id'=>$jz_uid))->getField('phone');
                //老师提交签到 自动通知家长签到
                $moban = "【老师上门】授课结束签到通知：".$signs['date']."日老师授课已结束,请家长登陆网站平台确认签到。";
                tongz_miaodi($js_phone,$moban);

                $arr = array(
                    'error' =>1,
                    'info' => '发送成功'  
                    );
                echo json_encode($arr); 
            }else {
                $arr = array(
                    'error' =>0,
                    'info' => '发送失败'  
                    );
                echo json_encode($arr); 
            }
        }
    }


    //家长预约
    public function jz_order_yuyue()
    {
        $id = I('get.id');
        //我的预约显示
        $sql = "select t1.*,t2.sex,t2.sfz_name,t2.phone from jz_to_ls_yuyue t1 inner join laoshi_user t2 on t1.ls_uid=t2.id where t1.jz_uid='{$id}' and is_shenhe=0";
        $yuyues = M()->query($sql);
        $this->assign('yuyues',$yuyues);

        $this->display();
    }

    //家长预定单 取消 删除
    public function jz_order_del()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('jz_to_ls_yuyue')->delete($id);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"取消成功"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"取消失败"
                        );
                echo json_encode($arr);
            }
        }
    }

	//家长订单
    public function jz_order()
    {
        $id = I('get.id');
        //我的订单显示
        $sql = "select t1.*,t2.sex,t2.sfz_name,t2.phone from jz_to_ls_yuyue t1 inner join laoshi_user t2 on t1.ls_uid=t2.id where t1.jz_uid='{$id}' and is_laoshi_on=1 and is_shenhe=1";
        $orders = M()->query($sql);
        $this->assign('orders',$orders);

        $this->display();
    }



    //老师试课
    public function ls_order_shike()
    {
        $id = I('get.id');
        //老师试课列表显示
        $sql = "select t1.*,t2.sex,t2.phone,t2.name from jz_to_ls_yuyue t1 inner join jiazhang_user t2 on t1.jz_uid=t2.id where t1.ls_uid='{$id}' and is_shenhe=0";
        $yuyues = M()->query($sql);
        $this->assign('yuyues',$yuyues);

        $this->display();
    }
    //老师预约订单 接受 
    public function jieshou_on()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('jz_to_ls_yuyue')->where(array('id'=>$id))->setField('is_laoshi_on',1);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"接受成功,请保持手机畅或连接课程顾问"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"接受失败,重新在接受"
                        );
                echo json_encode($arr);
            }
        }
    }
    //老师预约订单 取消 删除
    public function quxiao_off()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('jz_to_ls_yuyue')->delete($id);
            if ($res) {
                 $arr = array(
                        'error' =>1,
                        'info'=>"取消成功,删除订单"
                        );
                echo json_encode($arr);
            }else {
                 $arr = array(
                        'error' =>0,
                        'info'=>"取消失败"
                        );
                echo json_encode($arr);
            }
        }
    }

    //老师订单
    public function ls_order()
    {
        $id = I('get.id');
        //我的订单显示
        $sql = "select t1.*,t2.sex,t2.phone,t2.name from jz_to_ls_yuyue t1 inner join jiazhang_user t2 on t1.jz_uid=t2.id where t1.ls_uid='{$id}' and is_laoshi_on=1 and is_shenhe=1";
        $orders = M()->query($sql);
        $this->assign('orders',$orders);

        $this->display();
    }








}